package com.natalia.file.service.impl.minio;

import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.text.StrPool;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.natalia.file.entity.UploadInfo;
import org.apache.commons.lang3.ObjectUtils;

/**
 * @Author yang
 **/
public class PolicyJsonHelper {

    private PolicyJsonHelper(){

    }

    public static String get(String bucketName) {
        JsonObject policy = new JsonObject();
        policy.addProperty("Version", "2012-10-17");
        JsonArray statementArray = new JsonArray();
        JsonObject statement = new JsonObject();
        statement.addProperty("Effect", "Allow");
        statement.addProperty("Principal", "*");
        statement.addProperty("Action", "s3:GetObject");
        statement.addProperty("Resource", "arn:aws:s3:::" + bucketName + "/*");
        statementArray.add(statement);
        policy.add("Statement", statementArray);
        return policy.toString();
    }

    public static String getUrl(String endpoint, UploadInfo uploadInfo, String fileName) {
        String prefix = ObjectUtils.isNotEmpty(uploadInfo.getPathPrefix()) ? CharSequenceUtil.EMPTY : StrPool.SLASH;
        return endpoint + StrPool.SLASH + uploadInfo.getBucketName() + prefix + fileName;
    }
}
